//
//  FirstViewController.m
//  demo2-计算器
//
//  Created by Maggie on 15/8/8.
//  Copyright (c) 2015年 fmning. All rights reserved.
//

#import "FirstViewController.h"

//当前viewcontroller的extension，里面声明私有成员

@interface FirstViewController ()
{
    //声明成员变量
    NSMutableString *_currentString;
    int _value1;
    int _value2;
    NSString *_operator;
    
}

//属性
@property (weak, nonatomic) IBOutlet UILabel *resultLable;

@end

@implementation FirstViewController

#pragma mark - 视图加载函数
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //当前页面初始化操作
    _currentString = [[NSMutableString alloc]init];
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    //资源的释放
}

#pragma mark - 点击数字键触发的方法
//参数说明sender：指当前事件的触发者
- (IBAction)digitClicked:(id)sender {
    
    //获得当前点击的按钮对象
    UIButton *btn = (UIButton *)sender;
    
    //获取按钮的标题
    NSString *title = [btn currentTitle];
    
    //追加(OC中向nil对象发送消息不会报错)
    [_currentString appendString:title];
    
    //更新lable
    self.resultLable.text = _currentString;
}

#pragma mark - 点击操作符触发的方法
- (IBAction)operatorClicked:(UIButton *)sender {
    //把第一数存储下来
    _value1 =[_currentString intValue];
    
    //清空当前字符串
    [_currentString setString:@""];
    
    //存储操作符
    _operator = [sender currentTitle];
}

#pragma mark - 点击等号触发的方法
- (IBAction)calculateClied:(id)sender {
    //第二个数
    _value2 = [_currentString intValue];
    
    int result = 0;
    char c = [_operator characterAtIndex:0];
    
    switch (c) {
        case '+':
            result = _value1 + _value2;
            break;
        case '-':
            result = _value1 + _value2;
            break;
        case '/':
            result = _value1 / _value2;
            break;
        case '*':
            result = _value1 * _value2;
            break;
            
        default:
            break;
    }
    
    //结果显示到页面上
    self.resultLable.text = [[NSString alloc]initWithFormat:@"%d",result];
    
    
    
}

#pragma mark - 点击0触发的方法
- (IBAction)zeroClicke:(id)sender {
    
}

#pragma mark - 点击.触发的操作
- (IBAction)pointClicked:(id)sender {
    
}
#pragma mark - 点击A/C清除按钮触发的事件
- (IBAction)clearClicked:(UIButton *)sender {
    //清空
    [_currentString setString:@""];
    self.resultLable.text = _currentString;
}

@end





